NSString * const cellID = @"cellid";
#import "ZQVideoTableViewPage1.h"
#import "SDCycleScrollView.h"
#import "UIView+SDAutoLayout.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "SDRefresh.h"
#import "DemoVC7Model.h"
#import "DemoVC7Cell.h"
#import "DemoVC7Cell2.h"
@interface ZQVideoTableViewPage1 ()
{
    SDRefreshFooterView *_refreshFooter;
}
@property (nonatomic, strong) NSMutableArray *modelsArray;
@end
@implementation ZQVideoTableViewPage1
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupHeaderView];
    [self.tableView registerClass:[DemoVC7Cell class] forCellReuseIdentifier:NSStringFromClass([DemoVC7Cell class])];
    [self.tableView registerClass:[DemoVC7Cell2 class] forCellReuseIdentifier:NSStringFromClass([DemoVC7Cell2 class])];
    __weak typeof(self) weakSelf = self;
    SDRefreshHeaderView *refreshHeader = [SDRefreshHeaderView refreshView];
    [refreshHeader addToScrollView:self.tableView];
    __weak typeof(refreshHeader) weakRefreshHeader = refreshHeader;
    weakRefreshHeader.beginRefreshingOperation = ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self creatModelsWithCount:2];
            [weakSelf.tableView reloadData];
            [weakRefreshHeader endRefreshing];
        });
    };
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 70, 0);
    _refreshFooter = [SDRefreshFooterView refreshView];
    [_refreshFooter addToScrollView:self.tableView];
    NSLog(@"%@",NSStringFromCGRect(_refreshFooter.frame));
    __weak typeof(_refreshFooter) weakRefreshFooter = _refreshFooter;
    _refreshFooter.beginRefreshingOperation = ^() {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf creatModelsWithCount:20];
            [weakSelf.tableView reloadData];
            [weakRefreshFooter endRefreshing];
        });
    };
}
#pragma mark - === 设置HeaderView===
- (void)setupHeaderView
{
    UIView *headerView = [UIView new];
    headerView.width = SCREEN_WIDTH;
    NSArray *picImageNamesArray = @[ @"pic1.jpg",
                                     @"pic2.jpg",
                                     @"pic3.jpg",
                                     @"pic4.jpg",
                                     ];
    SDCycleScrollView *scrollView = [SDCycleScrollView new];
    scrollView.localizationImageNamesGroup = picImageNamesArray;
    [headerView addSubview:scrollView];
    UILabel *tagLabel = [UILabel new];
    tagLabel.font = [UIFont systemFontOfSize:13];
    tagLabel.textColor = [UIColor lightGrayColor];
    tagLabel.text = @"更新时间：2016.01.08";
    [headerView addSubview:tagLabel];
    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3];
    [headerView addSubview:bottomLine];
    scrollView.sd_layout
    .leftSpaceToView(headerView, 0)
    .topSpaceToView(headerView, 0)
    .rightSpaceToView(headerView, 0)
    .heightIs(250);
    tagLabel.sd_layout
    .leftSpaceToView(headerView, 10)
    .topSpaceToView(scrollView, 0)
    .rightSpaceToView(headerView, 0)
    .heightIs(30);
    bottomLine.sd_layout
    .leftSpaceToView(headerView, 0)
    .topSpaceToView(tagLabel, 0)
    .rightEqualToView(tagLabel)
    .heightIs(1);
    [headerView setupAutoHeightWithBottomView:bottomLine bottomMargin:0];
    [headerView layoutSubviews];
    self.tableView.tableHeaderView = headerView;
}
- (void)setupHeaderView2{
    UIView *header = [UIView new];
    header.width = [UIScreen mainScreen].bounds.size.width;
    NSArray *picImageNamesArray = @[ @"pic1.jpg",
                                     @"pic2.jpg",
                                     @"pic3.jpg",
                                     @"pic4.jpg",
                                     ];
    SDCycleScrollView *scrollView = [SDCycleScrollView new];
    scrollView.localizationImageNamesGroup = picImageNamesArray;
    [header addSubview:scrollView];
    UILabel *tagLabel = [UILabel new];
    tagLabel.font = [UIFont systemFontOfSize:13];
    tagLabel.textColor = [UIColor lightGrayColor];
    tagLabel.text = @"更新时间：2016.01.08";
    [header addSubview:tagLabel];
    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3];
    [header addSubview:bottomLine];
    scrollView.sd_layout
    .leftSpaceToView(header, 0)
    .topSpaceToView(header, 0)
    .rightSpaceToView(header, 0)
    .heightIs(150);
    tagLabel.sd_layout
    .leftSpaceToView(header, 10)
    .topSpaceToView(scrollView, 0)
    .heightIs(25)
    .rightSpaceToView(header, 0);
    bottomLine.sd_layout
    .topSpaceToView(tagLabel, 0)
    .leftSpaceToView(header, 0)
    .rightSpaceToView(header, 0)
    .heightIs(1);
    [header setupAutoHeightWithBottomView:bottomLine bottomMargin:0];
    [header layoutSubviews];
    self.tableView.tableHeaderView = header;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Class currentClass = [DemoVC7Cell class];
    DemoVC7Cell *cell = nil;
    DemoVC7Model *model = self.modelsArray[indexPath.row];
    if (indexPath.row % 2 == 0) {
        currentClass = [DemoVC7Cell class];
    }else {
        currentClass = [DemoVC7Cell2 class];
    }
    cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(currentClass)];
    cell.model = model;
    [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Class currentClass = [DemoVC7Cell class];
    DemoVC7Model *model = self.modelsArray[indexPath.row];
    if (indexPath.row % 2 == 0) {
        currentClass = [DemoVC7Cell class];
    }else {
        currentClass = [DemoVC7Cell2 class];
    }
    return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:currentClass contentViewWidth:[self cellContentViewWith]];
}
- (CGFloat)cellContentViewWith
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}
- (void)creatModelsWithCount:(NSInteger)count
{
    if (!_modelsArray) {
        _modelsArray = [NSMutableArray new];
    }
    NSArray *iconImageNamesArray = @[@"icon0.jpg",
                                     @"icon1.jpg",
                                     @"icon2.jpg",
                                     @"icon3.jpg",
                                     @"icon4.jpg",
                                     ];
    NSArray *textArray = @[@"当你的 app 没有提供 3x 的LaunchImage 时。然后等比例拉伸",
                           @"然后等比例拉伸到大屏。屏幕宽度返回 320否则在大屏上会显得字大",
                           @"长期处于这种模式下，否则在大屏上会显得字大，内容少这种情况下对界面不会",
                           @"但是建议不要长期处于这种模式下，否则在大屏上会显得字大，内容少，容易遭到用户投诉。",
                           @"屏幕宽度返回 320；然后等比例拉伸到大屏。这种情况下对界面不会产生任小。"
                           ];
    for (int i = 0; i < count; i++) {
        int iconRandomIndex = arc4random_uniform(5);
        int nameRandomIndex = arc4random_uniform(5);
        DemoVC7Model *model = [DemoVC7Model new];
        model.title = textArray[nameRandomIndex];
        int random = arc4random_uniform(100);
        if (random <= 30) {
            NSMutableArray *temp = [NSMutableArray new];
            for (int i = 0; i < 3; i++) {
                int randomIndex = arc4random_uniform(5);
                NSString *text = iconImageNamesArray[randomIndex];
                [temp addObject:text];
            }
            model.imagePathsArray = [temp copy];
        } else {
            model.imagePathsArray = @[iconImageNamesArray[iconRandomIndex]];
        }
        [self.modelsArray addObject:model];
    }
}
@end
